package com.bdqn.vueboot.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bdqn.vueboot.config.BascText;
import com.bdqn.vueboot.dto.SuppliesDto;
import com.bdqn.vueboot.entity.Static;
import com.bdqn.vueboot.entity.Supplies;
import com.bdqn.vueboot.entity.SysUser;
import com.bdqn.vueboot.mapper.StaticMapper;
import com.bdqn.vueboot.mapper.SuppliesMapper;
import com.bdqn.vueboot.mapper.SysUserMapper;
import com.bdqn.vueboot.service.SuppliesService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 办公物品申领(Supplies)表服务实现类
 *
 * @author makejava
 * @since 2022-12-03 09:09:36
 */
@Service("suppliesService")
public class SuppliesServiceImpl extends ServiceImpl<SuppliesMapper, Supplies> implements SuppliesService {
    @Resource
    SuppliesMapper suppliesMapper;
    @Resource
    StaticMapper staticMapper;
    @Resource
    SysUserMapper sysUserMapper;

    @Override
    public IPage<SuppliesDto> pages(IPage<Supplies> page, SuppliesDto SuppliesDto) {
        LambdaQueryWrapper<Supplies> lambdaQueryWrapper= new LambdaQueryWrapper<>();
        lambdaQueryWrapper.like(SuppliesDto.getSuppliesName()!=null&&SuppliesDto.getSuppliesName()!="",Supplies::getSuppliesName,SuppliesDto.getSuppliesName());
        IPage<Supplies> page1 = suppliesMapper.selectPage(page, lambdaQueryWrapper);
        IPage<SuppliesDto>page2=new Page<>();
        BeanUtils.copyProperties(page1,page2);
        List<Supplies> records = page1.getRecords();
        List<SuppliesDto> suppliesDtoList=new ArrayList<SuppliesDto>();
        for (Supplies supplies:records
             ) {
            SuppliesDto suppliesDto=new SuppliesDto();
            BeanUtils.copyProperties(supplies,suppliesDto);
            suppliesDtoList.add(suppliesDto);
        }

        suppliesDtoList=suppliesDtoList.stream().map((item) -> {
            Static aStatic = staticMapper.selectById(item.getSuppliesStaticId());
            item.setSuppliesStaticName(aStatic.getStaticName());
            SysUser sysUser = sysUserMapper.selectById(item.getSuppliesProposerId());
            item.setSuppliesProposerName(sysUser.getUsrName());
            return  item;
        }).collect(Collectors.toList());
        page2.setRecords(suppliesDtoList);
        return page2;
    }

    @Override
    public int deleteById(Long suppliesId) {
        Supplies supplies =new Supplies();
        supplies.setSuppliesId(suppliesId);
        supplies.setSuppliesStaticId(50L);
        int i = suppliesMapper.updateById(supplies);

        return i;
    }

    @Override
    public int add(Supplies supplies) {
        supplies.setSuppliesStaticId(52L);
        supplies.setSuppliesProposerId(BascText.getCarId());
        supplies.setSuppliesId(null);
        int insert = suppliesMapper.insert(supplies);
        return insert;
    }
}

